package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SaleListGoodsService;
import com.atguigu.jxc.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;


    /**
     * 分页查询商品库存信息
     * @param codeOrName 商品编码或名称
     * @param goodsTypeId 商品类别ID
     * @return
     */
    @Override
    public List<Goods> listInventory(String codeOrName, Integer goodsTypeId,Integer page,Integer rows) {
        //PageHelper.startPage(page,rows);
        List<Goods> goods = goodsDao.selectPageInventory(codeOrName, goodsTypeId);
        if (goods.size() == 0){
            goods=goodsDao.selectPageInventorycode(codeOrName, goodsTypeId);
        }
        //PageInfo<Goods> pageInfo = new PageInfo<>(goods);
        return goods;
    }

    /**
     * 查询商品信息
     * @param goodsTypeId
     * @param goodsName
     * @return
     */
    @Override
    public List<Goods> list(Integer goodsTypeId, String goodsName) {


        return goodsDao.selectGoodsList(goodsTypeId,goodsName);
    }

    /**
     *商品添加或修改
     */
    @Override
    public void saveAndUpdate(Goods goods) {
        Goods goods1 = goodsDao.selectGoodsGoodsCode(goods.getGoodsCode());
        if (goods1 == null){
            //是空为添加
            goodsDao.insetGoods(goods);
        }else {
            //否则为修改
            goodsDao.UpdateGoods(goods);
        }
    }
    /**\
     * 商品删除
     */
    @Override
    public ServiceVO deleteGoodsById(Integer goodsId) {
        ServiceVO serviceVO = null;
        Goods goods = goodsDao.selectGoodsById(goodsId);
        if (goods == null){
             serviceVO = new ServiceVO(200,"请求失败，没有该商品",null);
            return serviceVO;
        } else if (goods.getState()==1) {
            serviceVO = new ServiceVO(ErrorCode.STORED_ERROR_CODE,ErrorCode.STORED_ERROR_MESS,null);
            return serviceVO;
        } else if (goods.getState()==2) {
            serviceVO = new ServiceVO(ErrorCode.HAS_FORM_ERROR_CODE,ErrorCode.HAS_FORM_ERROR_MESS,null);
            return serviceVO;
        }
        goodsDao.deleteGoodsById(goodsId);

        serviceVO = new ServiceVO(100,"请求成功",null);
        return serviceVO;
    }

    /**
     * 无库存商品列表展示（可以根据商品名称或编码查询）
     */
    @Override
    public List<Goods> getNoInventoryQuantity(String nameOrCode) {
        List<Goods> list = null;
        list = goodsDao.selectGoodsByNameOrCode(nameOrCode);
        if (list.size()<=0){
            list = goodsDao.selectGoodsByName(nameOrCode);
        }
        return list;
    }
    /**
     * 有库存商品列表展示（可以根据商品名称或编码查询）
     */
    @Override
    public List<Goods> getHasInventoryQuantity(String nameOrCode) {
        List<Goods> list = null;
        list = goodsDao.selectGoodsByNameOrCodeYoukucun(nameOrCode);
        if (list.size()<=0){
            list = goodsDao.selectGoodsByNameYoukucun(nameOrCode);
        }
        return list;
    }

    /**
     * 添加库存、修改数量或成本价
     * @param goodsId
     * @param inventoryQuantity
     * @param purchasingPrice
     */
    @Override
    public void saveStock(Integer goodsId, Integer inventoryQuantity, Double purchasingPrice) {
        goodsDao.UpdateGoodsSaveStock(goodsId,inventoryQuantity,purchasingPrice);
    }

    /**
     * 查询所有 当前库存量 小于 库存下限的商品信息
     */
    @Override
    public List<Goods> listAlarm() {
        return goodsDao.selectList();
    }


//    @Override
//    public Map<String, Goods> listInventoryone() {
//
//        return goodsDao.listInventoryone();
//    }


    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for(int i = 4;i > intCode.toString().length();i--){

            unitCode = "0"+unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }




}
